package com.bball.court.controller.parse;

import java.util.concurrent.CountDownLatch;

import android.test.InstrumentationTestCase;
import android.util.Log;

import com.bball.court.controller.CourtController;
import com.bball.court.http.AddCourtRequestCallbacks;
import com.bball.court.http.GetCourtsRequestCallbacks;
import com.bball.court.model.Court;
import com.bball.court.model.CourtList;
import com.bball.court.model.parse.ParseCourt;
import com.parse.Parse;
import com.parse.ParseObject;

public class ParseCourtControllerTest extends InstrumentationTestCase {

    private static final String NAME1 = "Job";
    private static final String NAME2 = "Maison";
    private static final String CITY1 = "Montreal";
    private static final String CITY2 = "Montreal";
    private static final String ADRESS1 = "740 Rue Notre-Dame Ouest";
    private static final String ADRESS2 = "45 rue du Maréchal Foch";

    private static final String CLIENT_KEY = "NaBMEGW8KLwkaIFhzABiRJOzFYhpq7JZakj4heKc";
    private static final String APP_ID = "u5ku5ZMBw0KtOQRXv3xU82WU5GgEx9v8C5mLThkH";

    private double latitude = 45.499384;
    private double longitude = -73.56028;

    private ParseCourt court1;
    private ParseCourt court2;
    private CourtController parseCourtController;
    private CountDownLatch signal;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        ParseObject.registerSubclass(ParseCourt.class);
        Parse.initialize(getInstrumentation().getTargetContext(), APP_ID, CLIENT_KEY);

        court1 = new ParseCourt();
        court1.setName(NAME1);
        court1.setAddress(ADRESS1);
        court1.setCountry("Canada");
        court1.setCity(CITY1);
        court1.setGeolocation(43.240332, 0.052628);

        court2 = new ParseCourt();
        court2.setName(NAME2);
        court2.setCountry("France");
        court2.setCity(CITY2);
        court2.setAddress(ADRESS2);
        court2.setGeolocation(45.536609, -73.630261);

        parseCourtController = new ParseCourtController(getInstrumentation().getTargetContext());
    }

    public void test1SuggestCourt() throws InterruptedException {
        signal = new CountDownLatch(2);
        parseCourtController.addSuggestedCourt(court1.getName(), null, court1.getLatLng(), new String[] { court1.getAddress() });
        parseCourtController.addSuggestedCourt(court2.getName(), null, court2.getLatLng(), new String[] { court2.getAddress() });
        signal.await();
    }

    public void test2FindCourt() throws InterruptedException {
        signal = new CountDownLatch(1);
        parseCourtController.findClosestCourt(getCourtsRequestCallbacks, latitude, longitude);
        signal.await();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        court1.delete();
        court2.delete();
    }

    private GetCourtsRequestCallbacks getCourtsRequestCallbacks = new GetCourtsRequestCallbacks() {

        @Override
        public void onGetCourtsRequestFailed(RuntimeException runtimeException) {
            fail();
        }

        @Override
        public void onGetCourtsRequestComplete(CourtList courts) {
            assertEquals(11, courts.size());
            signal.countDown();
        }
    };

    private AddCourtRequestCallbacks addCourtRequestCallback = new AddCourtRequestCallbacks() {

        @Override
        public void onAddCourtSuccess(Court court) {
            Log.i(getClass().getSimpleName(), court.getName() + " added");
            signal.countDown();
        }

        @Override
        public void onAddCourtFail(RuntimeException runtimeException) {
            Log.e(getClass().getSimpleName(), runtimeException.getMessage(), runtimeException);
            fail();
        }
    };

}
